package com.youkeyi.ddy.cloud.basic.crm.follow.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.follow.entity.SysCustomerFollowUp;
import com.youkeyi.ddy.cloud.basic.crm.follow.service.SysCustomerFollowUpService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.CrmCustomerFollowUpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.dto.CrmCustomerFollowUpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.dto.CrmCustomerFollowUpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户跟进情况 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-01
 */
@Service
public class SysCustomerFollowUpController implements CrmCustomerFollowUpCloudService {
    
    @Autowired
    private SysCustomerFollowUpService sysCustomerFollowUpService;

    @Override
    public CrmCustomerFollowUpListCloudDto query(CrmCustomerFollowUpQueryCloudParam param) {
        CrmCustomerFollowUpListCloudDto dto = new CrmCustomerFollowUpListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCustomerFollowUpPrimaryKeys())) {
            param.getCustomerFollowUpPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerKeys())) {
            param.getCustomerKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getStatuses())) {
            param.getStatuses().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomerFollowUp> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCustomerFollowUpPrimaryKeys()), SysCustomerFollowUp::getPrimaryKey, param.getCustomerFollowUpPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomerFollowUp::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerKeys()), SysCustomerFollowUp::getCustomerKey, param.getCustomerKeys())
                .in(CollectionUtils.isNotEmpty(param.getStatuses()), SysCustomerFollowUp::getStatus, param.getStatuses())
                .ne(SysCustomerFollowUp::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysCustomerFollowUp> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCustomerFollowUp> customerFollowUpList = sysCustomerFollowUpService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerFollowUpList.getRecords())) {
            dto.setTotal(customerFollowUpList.getTotal());

            List<CrmCustomerFollowUpListItemCloudDto> items = new ArrayList<>();
            customerFollowUpList.getRecords().forEach(customerFollowUp -> {
                CrmCustomerFollowUpListItemCloudDto item = new CrmCustomerFollowUpListItemCloudDto();
                item.setCustomerFollowUpPrimaryKey(customerFollowUp.getPrimaryKey());
                item.setCorpKey(customerFollowUp.getCorpKey());
                item.setCustomerKey(customerFollowUp.getCustomerKey());
                item.setFollowUp(customerFollowUp.getFollowUp());
                item.setFollowUpDate(customerFollowUp.getFollowUpDate() != null ? customerFollowUp.getFollowUpDate().getTime() : null);
                item.setNextFollowUpRemindDate(customerFollowUp.getNextFollowUpRemindDate() != null ? customerFollowUp.getNextFollowUpRemindDate().getTime() : null);
                item.setRemindStart(customerFollowUp.getRemindStart());
                item.setRemark(customerFollowUp.getRemark());
                item.setStatus(customerFollowUp.getStatus());
                item.setCustomerTypeKey(customerFollowUp.getCustomerTypeKey());
                item.setServerDate(null != customerFollowUp.getServerDate() ? customerFollowUp.getServerDate().getTime() : null);
                item.setAssistingPersonnelKey(customerFollowUp.getAssistingPersonnelKey());
                item.setAssistingPersonnelName(customerFollowUp.getAssistingPersonnelName());
                item.setIsDeleted(customerFollowUp.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customerFollowUp.getCreateTime() != null ? customerFollowUp.getCreateTime().getTime() : null);
                item.setCreateUserKey(customerFollowUp.getCreateUserKey());
                item.setCreateUserName(customerFollowUp.getCreateUserName());
                item.setUpdateTime(customerFollowUp.getUpdateTime() != null ? customerFollowUp.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customerFollowUp.getUpdateUserKey());
                item.setUpdateUserName(customerFollowUp.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerFollowUpCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getCustomerKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomerFollowUp customerFollowUp = new SysCustomerFollowUp();
        customerFollowUp.setPrimaryKey(SimpleUUidUtils.getUuid());
        customerFollowUp.setCorpKey(param.getCorpKey());
        customerFollowUp.setCustomerKey(param.getCustomerKey());
        customerFollowUp.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate() != null ? new Date(param.getNextFollowUpRemindDate()) : null);
        customerFollowUp.setRemindStart(param.getRemindStart());
        customerFollowUp.setRemark(param.getRemark());
        customerFollowUp.setFollowUp(param.getFollowUp());
        customerFollowUp.setFollowUpDate(null != param.getFollowUpDate() ? new Date(param.getFollowUpDate()) : null);
        customerFollowUp.setStatus(param.getStatus());
        customerFollowUp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customerFollowUp.setCustomerTypeKey(param.getCustomerTypeKey());
        customerFollowUp.setServerDate(null != param.getServerDate() ? new Date(param.getServerDate()) : null);
        customerFollowUp.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        customerFollowUp.setAssistingPersonnelName(param.getAssistingPersonnelName());
        customerFollowUp.setCreateUserKey(param.getCreateUserKey());
        customerFollowUp.setCreateUserName(param.getCreateUserName());
        customerFollowUp.setCreateTime(new Date());
        boolean save = sysCustomerFollowUpService.save(customerFollowUp);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customerFollowUp.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerFollowUpUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerFollowUpPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomerFollowUp> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomerFollowUp::getPrimaryKey, param.getCustomerFollowUpPrimaryKey());
        SysCustomerFollowUp customerFollowUp = new SysCustomerFollowUp();
        customerFollowUp.setPrimaryKey(param.getCustomerFollowUpPrimaryKey());
        customerFollowUp.setFollowUp(param.getFollowUp());
        customerFollowUp.setFollowUpDate(null != param.getFollowUpDate() ? new Date(param.getFollowUpDate()) : null);
        customerFollowUp.setStatus(param.getStatus());
        customerFollowUp.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate() != null ? new Date(param.getNextFollowUpRemindDate()) : null);
        customerFollowUp.setRemindStart(param.getRemindStart());
        customerFollowUp.setRemark(param.getRemark());
        customerFollowUp.setCustomerTypeKey(param.getCustomerTypeKey());
        customerFollowUp.setServerDate(null != param.getServerDate() ? new Date(param.getServerDate()) : null);
        customerFollowUp.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        customerFollowUp.setAssistingPersonnelName(param.getAssistingPersonnelName());
        customerFollowUp.setUpdateUserKey(param.getUpdateUserKey());
        customerFollowUp.setUpdateUserName(param.getUpdateUserName());
        customerFollowUp.setUpdateTime(new Date());
        boolean save = sysCustomerFollowUpService.update(customerFollowUp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmCustomerFollowUpDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCustomerFollowUpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCustomerFollowUp> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCustomerFollowUp::getPrimaryKey, param.getCustomerFollowUpPrimaryKeys());

        SysCustomerFollowUp customerFollowUp = new SysCustomerFollowUp();
        customerFollowUp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        customerFollowUp.setUpdateUserKey(param.getUpdateUserKey());
        customerFollowUp.setUpdateUserName(param.getUpdateUserName());
        customerFollowUp.setUpdateTime(new Date());
        boolean save = sysCustomerFollowUpService.update(customerFollowUp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
